/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： OperatorIdArgumentResolver.java
 * Date：18-11-5 上午10:54
 * Author: guomw
 */

package com.guomw.report.service.resolver;

import com.guomw.report.service.annotations.OperatorId;
import com.guomw.report.service.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author guomw
 * @date 2018/9/21
 */
public class OperatorIdArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private SystemService systemService;

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return  methodParameter.getParameterAnnotation(OperatorId.class)!=null && methodParameter.getParameterType().isAssignableFrom(Integer.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return systemService.getCurrentRootId(nativeWebRequest.getNativeRequest(HttpServletRequest.class));
    }
}
